package com.yanqu.road.server.logic.activity.condition.specialserver;

import com.yanqu.road.entity.autodata.specialserver.IceServerUserData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.specialserver.IceServerModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.task.BaseCondition;
import com.yanqu.road.server.logic.task.BaseTaskData;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

public class IceServerUpgradeAllBuildingCondition extends BaseCondition {

    public IceServerUpgradeAllBuildingCondition(TaskConditionInfo taskConditionInfo, BaseTaskData baseTaskData, BigInteger value, Object initArgs) {
        super(taskConditionInfo, baseTaskData, value, initArgs);
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.IceServerUpgradeAllBuilding.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.IceServerUpgradeAllBuilding.getValue(), handle);
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        return getValue().compareTo(BigInteger.ZERO) > 0;
    }

    @Override
    protected void changeValue(Object object) {
        initValue();
    }

    @Override
    public void initValue() {
        // 获取最大等级 所有建筑
        Map<Integer, Integer> buildingLevelMap = new HashMap<>();
        Map<Integer, BaseTaskData> baseTaskDataMap = player.getModule(TaskModule.class).getBaseTaskDataMap();
        for (BaseTaskData baseTaskData : baseTaskDataMap.values()) {
            if (baseTaskData == null || baseTaskData.getBaseCondition() == null || baseTaskData.getBaseCondition().getTaskConditionInfo() == null) {
                continue;
            }
            if (baseTaskData.getBaseCondition().getTaskConditionInfo().getType() != eGamePlayerEventType.IceServerUpgradeBuilding.getValue()) {
                continue;
            }
            int buildingLevel = 0;
            int buildingId = baseTaskData.getBaseCondition().getTaskConditionInfo().getParamList().get(2).intValue();
            // 已完成
            if (baseTaskData.getUserTaskData().getState() == 1) {
                buildingLevel = baseTaskData.getBaseCondition().getTaskConditionInfo().getParamList().get(0).intValue();
            } else if (baseTaskData.getUserTaskData().getPreTaskId() != 0) {  // 当前任务未完成的话且上一条任务不为0，则等级-1就行了，有问题找数值
                buildingLevel = baseTaskData.getBaseCondition().getTaskConditionInfo().getParamList().get(0).intValue() - 1;
            }
            buildingLevel = Math.max(buildingLevel, buildingLevelMap.getOrDefault(buildingId, 0));
            buildingLevelMap.put(buildingId, buildingLevel);
        }

        if (buildingLevelMap.isEmpty()) {
            return;
        }
        int minLevel = Integer.MAX_VALUE;
        for (Integer buildingLevel : buildingLevelMap.values()) {
            minLevel = Math.min(minLevel, buildingLevel);
        }
        if (minLevel >= taskConditionInfo.getParamList().get(0).intValue()) {
            setValue(BigInteger.ONE);
        }

        // 记录一下达成时间
        if (isCompleted(player)) {
            boolean needSync = false;
            IceServerUserData userData = player.getModule(IceServerModule.class).getUserData();
            if (userData != null) {
                if (!userData.getTaskCompleteTimeMap().containsKey(taskConditionInfo.getTaskId())) {
                    userData.getTaskCompleteTimeMap().put(taskConditionInfo.getTaskId(), System.currentTimeMillis());
                    userData.setUpdateOption();
                    needSync = true;
                }
            }
            if (needSync) {
                player.getModule(IceServerModule.class).syncUserData();
            }
        }
    }
}
